//
//  TTCommonNetData.m
//  TaoTuo
//
//  Created by admin on 2019/8/30.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTCommonNetData.h"
#import "TTOrderModel.h"
#import "TTOrderShopModel.h"

@implementation TTCommonNetData

+ (void)getShopCartCodeInfoSuccess:(void (^)(NSArray * _Nonnull, int))success failure:(dispatch_block_t)failure
{
    if (![TTLoginSingle sharedSingle].isLogin) {
        success(@[], 0);
        return;
    }
    
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:getShopBarCode_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        [self handleShopCartCodeInfoWithArray:responseObject];
        
        success([TTLoginSingle sharedSingle].cartCodeArray, [TTLoginSingle sharedSingle].shopCartCount);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        failure();
        
    } showLoadingInView:nil];
}

+ (void)handleShopCartCodeInfoWithArray:(NSArray *)responseObject
{
    int count = 0;
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dic in responseObject) {
        TTShopCartCodeModel *model = [TTShopCartCodeModel modelWithJSON:dic];
        count += model.Number;
        [arr addObject:model];
    }
    // 购物车商品条码
    [TTLoginSingle sharedSingle].cartCodeArray = arr;
    [TTLoginSingle sharedSingle].shopCartCount = count;
    
    // 购物车数量
    NSString *shopCount = nil;
    if (count > 0) {
        shopCount = [NSString stringWithFormat:@"%d", count];
    }
    
//    UITabBarController *tabbarVC = [LLRouteTool getRootViewController];
//    [tabbarVC.tabBar showBadgeValue:shopCount atIndex:3];
    
}

+ (void)getVipLevelSuccess:(void (^)(TTUserModel * _Nonnull))success failure:(dispatch_block_t)failure
{
    TTLoginSingle *single = [TTLoginSingle sharedSingle];
    if (!single.isLogin) {
        failure();
        return;
    }
    
    
    NSDictionary *dic = nil;
    if (single.loginModel.InviteCode.length > 0) {
        dic = @{
                @"param.popularizeCode" : single.loginModel.InviteCode
                };
    }
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:getVipLevel_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        TTUserModel *userModel = [TTUserModel modelWithJSON:responseObject];
        single.userModel = userModel;
        
        success(userModel);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        failure();
    } showLoadingInView:nil];
}



// 获取订单列表
+ (void)getOrderListWithOrderState:(int)orderState page:(int)page success:(void (^)(NSURLSessionDataTask * _Nonnull, NSMutableArray * _Nonnull))success failure:(void (^)(NSURLSessionDataTask * _Nonnull, NSError * _Nullable))failure showLoadingInView:(UIView *)loadingView
{
    NSMutableDictionary *dic = @{
        @"param.pageIndex" : @(page),
        @"param.pageSize" : @40,
    }.mutableCopy;
    
    // orderState = 0 是全部订单
    if (orderState != 0) {
        [dic setObject:@(orderState) forKey:@"param.orderState"];
    }
    
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:orderList_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableArray *array = [self handleOrderData:responseObject];
        
        success(task, array);
        

    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {

        failure(task, error);
        
    } showLoadingInView:loadingView];
}


/// 获取个人店铺订单列表
+ (void)getDianpuOrderListWithOrderState:(NSInteger)orderState page:(int)page success:(void (^)(NSURLSessionDataTask * _Nonnull, NSMutableArray * _Nonnull))success failure:(void (^)(NSURLSessionDataTask * _Nonnull, NSError * _Nullable))failure showLoadingInView:(UIView *)loadingView
{
    NSDictionary *dic = @{
        @"param.pageIndex" : @(page),
        @"param.pageSize" : @40
    };
    NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    if (orderState < 3) {
        // 如果是全部的话，不传这个参数
        [mDic setObject:@(orderState) forKey:@"param.orderState"];
    }
    
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:plusOrderList_api parameters:mDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableArray *array = [self handleOrderData:responseObject];
        
        success(task, array);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        failure(task, error);
        
    } showLoadingInView:loadingView];
}

+ (NSMutableArray *)handleOrderData:(id)responseObject
{
    NSMutableArray *array = [NSMutableArray array];
    CGFloat cellImageH = 65 * kScale + 40;
    
    NSArray *arr = responseObject;
    for (NSDictionary *dic in arr) {
        TTOrderModel *model = [TTOrderModel modelWithJSON:dic];
        
        // 计算高度
        CGFloat totalCellH = 0;
        for (TTOrderShopModel *shopModel in model.Detail) {
            if (shopModel.GoodsType == 1) {
                shopModel.h = cellImageH + 10;
            } else {
                NSData *data = [shopModel.GoodsDescribe dataUsingEncoding:NSUTF8StringEncoding];
                NSArray *describe = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSString *content = [describe componentsJoinedByString:@"\n"];
                CGFloat contentH = [content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 65 * kScale - 40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} context:nil].size.height + 68 + 10;
                
                contentH = contentH < cellImageH ? cellImageH : contentH;
                
                shopModel.h = contentH;
                
                if (shopModel.GoodsType == 2) {
                    shopModel.h = contentH + 15;
                }
            }
            
            totalCellH += shopModel.h;
        }

        totalCellH += 105;
        if (model.OrderRecord.IsShow) {
            totalCellH += 40;
        }
        model.H = totalCellH;
        
        [array addObject:model];
    }
    
    return array;
}


@end
